package com.ln.asset.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ln.asset.core.pojo.Dict;
import com.ln.asset.core.mapper.DictMapper;
import com.ln.asset.core.service.DictService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 数据字典 服务实现类
 * </p>
 *
 * @author 卢光云
 * @since 2022-04-28
 */
@Slf4j
@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements DictService {
    @Resource
    RedisTemplate redisTemplate;
    @Override
    public List<Dict> listByParentId(Integer parentId) {
        //先查询redis中是否存在数据列表
        List<Dict> assList = null;
        try {
            assList = (List<Dict>)redisTemplate.opsForValue().get("asset:core:assList:" + parentId);
            if(assList != null){
                log.info("从redis中取值");
                return assList;
            }
        } catch (Exception e) {
            log.error("redis服务器异常：" + ExceptionUtils.getStackTrace(e));//此处不抛出异常，继续执行后面的代码
        }

        log.info("从数据库中取值");
        assList = baseMapper.selectList(new QueryWrapper<Dict>().eq("parent_id", parentId));
        assList.forEach(ass -> {
            //如果有子节点，则是非叶子节点
            boolean hasChildren = this.hashChildren(ass.getId());
            ass.setHasChildren(hasChildren);
        });
        //将数据存入redis
        try {
            redisTemplate.opsForValue().set("asset:core:assList:" + parentId, assList, 5, TimeUnit.MINUTES);
            log.info("数据存入redis");
        } catch (Exception e) {
            log.error("redis服务器异常：" + ExceptionUtils.getStackTrace(e));//此处不抛出异常，继续执行后面的代码
        }
        return assList;
    }

    @Override
    public List<Dict> findByDictCode(String dictCode) {
        QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dict_code", dictCode);
        Dict assetCategory = baseMapper.selectOne(queryWrapper);
        return this.listByParentId(assetCategory.getId());
    }

    private boolean hashChildren(Integer id){
        QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",id);
        Integer  count = baseMapper.selectCount(queryWrapper);
        if (count.intValue()>0){
            return true;
        }else {
            return false;
        }
    }



/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cuixiaoxin
 * @since 2022-04-29
 */

    //根据id查询所有pid的值
    @Override
    public List<Dict> selectDictByPid(Integer id) {
        QueryWrapper<Dict> dictQueryWrapper = new QueryWrapper<Dict>();
        dictQueryWrapper.eq("parent_id", id);
        List<Dict> dictList = baseMapper.selectList(dictQueryWrapper);
        return dictList;
    }
    //根据pid和value查询所有id的值
    @Override
    public List<Dict> selectDictById(Integer pid,String value) {
        QueryWrapper<Dict> dictQueryWrapper = new QueryWrapper<Dict>();

        dictQueryWrapper.eq("pid", pid)
                .eq("value",value);
        List<Dict> dictList = baseMapper.selectList(dictQueryWrapper);
        return dictList;
    }

    @Override
    public Dict selectDictByIdNuM(Integer id) {
        Dict dict =baseMapper.selectById(id);
        return dict;
    }

    @Override
    public Integer updateDictByIds(Dict dict) {
        Integer num1=baseMapper.updateById(dict);
        return num1;
    }

    @Override
    public Dict selectDictByDict(String dictcode, String value) {
        QueryWrapper<Dict> dictQueryWrapper = new QueryWrapper<Dict>();
        dictQueryWrapper.eq("dict_code", dictcode);
        Dict dictList = baseMapper.selectOne(dictQueryWrapper);
        QueryWrapper<Dict> dictQueryWrapper1 = new QueryWrapper<Dict>();
        dictQueryWrapper1.eq("parent_id", dictList.getId())
                .eq("value",value);
        Dict dict1=baseMapper.selectOne(dictQueryWrapper1);
        return dict1;
    }

    @Override
    public List<Dict> queryDictList() {
        List<Dict> dictList = baseMapper.selectList(null);
        return dictList;
    }

    @Override
    public List<Dict> selectDictLingJianByPid(Integer id)
    {
        QueryWrapper<Dict> dictQueryWrapper = new QueryWrapper<Dict>();
        dictQueryWrapper.eq("ispart", id);
        List<Dict> dictList = baseMapper.selectList(dictQueryWrapper);
        return dictList;
    }

    @Override
    public Dict selectDictLingJianByDict(String dictcode, String value) {
        QueryWrapper<Dict> dictQueryWrapper = new QueryWrapper<Dict>();
        dictQueryWrapper.eq("dict_code", dictcode);
        Dict dictList = baseMapper.selectOne(dictQueryWrapper);
        QueryWrapper<Dict> dictQueryWrapper1 = new QueryWrapper<Dict>();
        dictQueryWrapper1.eq("ispart", dictList.getId())
                .eq("partvalue",value);
        Dict dict1=baseMapper.selectOne(dictQueryWrapper1);

        return dict1;
    }
}

